package com.reny.szkj.ft_home.ui.index.home

import androidx.recyclerview.widget.LinearLayoutManager
import com.reny.ll.git.base_logic.MFragment
import com.reny.ll.git.base_logic.ext.checkLogin
import com.reny.ll.git.base_logic.model.login.LoginInfo
import com.reny.ll.git.mvvm.extras.observe
import com.reny.szkj.ft_home.R
import com.reny.szkj.ft_home.databinding.FragmentHomeBinding
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.kodein.di.DI
import org.kodein.di.instance

class HomeFragment: MFragment<FragmentHomeBinding>() {

    override val di by DI.lazy {
        extend(parentDI)
        import(homeDIModule)
    }

    override fun useEventBus(): Boolean = true

    override val layoutId: Int = R.layout.fragment_home

    override val viewModel: HomeViewModel by instance()

    private lateinit var adapter: HomeListAdapter

    override fun initView() {
        binding.rv.layoutManager = LinearLayoutManager(requireContext())
        adapter = HomeListAdapter()
        binding.rv.adapter = adapter

        observe(viewModel.liveResult){
            adapter.addDatas(it, viewModel.isRefresh)
        }
    }

    override fun lazyLoad() {
        checkLogin({
            viewModel.loadData(true)
        })
    }


    @Subscribe(threadMode = ThreadMode.MAIN)/*(sticky = true)*/
    fun onEvent(event: LoginInfo){
        lazyLoad()
    }

}